package cn.wolfcode.service.impl;

import cn.wolfcode.domain.Permission;
import cn.wolfcode.domain.Role;
import cn.wolfcode.domain.Student;
import cn.wolfcode.mapper.PermissionMapper;
import cn.wolfcode.mapper.RoleMapper;
import cn.wolfcode.qo.QueryObject;
import cn.wolfcode.service.IRoleService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
@Transactional

public class RoleServiceImpl implements IRoleService {

    @Autowired
    private RoleMapper roleMapper;

    @Autowired
    private PermissionMapper permissionMapper;

    @Override
    public void save(Role role,Long[] permissionIds) {
        Long roleId = role.getId();
        if (roleId == null) {
            roleMapper.insert(role);
            roleId = role.getId();
            //中间表插入
//            for (Long permissionId : permissionIds) {
//                roleMapper.insertToMiddleTable(roleId, permissionId);
//            }
            if (permissionIds != null && permissionIds.length > 0) {
                roleMapper.insertRelationBatch(role.getId(), permissionIds);
            }
        }else {
            roleMapper.updateByPrimaryKey(role);
            //根据roleId删除中间表信息
            roleMapper.deleteToMiddleTable(roleId);
            //向中间表插入内容
//            for (Long permissionId : permissionIds) {
//                roleMapper.insertToMiddleTable(roleId, permissionId);
//            }
            if (permissionIds != null && permissionIds.length > 0) {
                roleMapper.insertRelationBatch(role.getId(), permissionIds);
            }
        }
    }

    @Override
    public void delete(Long id) {
        roleMapper.deleteByPrimaryKey(id);
        roleMapper.deleteToMiddleTable(id);
    }


    @Override
    public Role get(Long id) {
        return roleMapper.selectByPrimaryKey(id);
    }

    @Override
    public List<Role> listAll() {
        return roleMapper.selectAll();
    }

    @Override
//    public ResultPage<Role> query(QueryObject qo) {
//        int totalCount = roleMapper.selForCount(qo);
//        if (totalCount == 0) {
//            return new ResultPage<>(qo.getCurrentPage(), qo.getPageSize());
//        }
//        List<Role> data = roleMapper.selForList(qo);
//        return new ResultPage<>(qo.getCurrentPage(), qo.getPageSize(), totalCount, data);
//
//    }
    public PageInfo<Role> query(QueryObject qo) {
        //使用分页插件
        PageHelper.startPage(qo.getCurrentPage(), qo.getPageSize());
        List<Role> roles = roleMapper.selForList(qo);
        return new PageInfo<>(roles);
    }

    @Override
    public List<Permission> selRolePermission(Long roleId) {
        return permissionMapper.selRolePermission(roleId);
    }

    /***
     * 根据员工id查询role
     * @param id
     * @return
     */
    @Override
    public List<Role> selEmployeeRoleById(Long id) {
        return roleMapper.selEmployeeRoleById(id);
    }

    /**
     * 通过学生id遍历学生角色信息
     * @param id
     * @return
     */
    @Override
    public List<Role> selRolesByStudentId(Long id) {
        return roleMapper.selRolesByStudentId(id);
    }
}
